{
 "nbformat": 4,
 "nbformat_minor": 0,
 "metadata": {
  "colab": {
   "name": "highway-planning.ipynb",
   "version": "0.3.2",
   "provenance": []
  },
  "kernelspec": {
   "name": "python3",
   "language": "python",
   "display_name": "Python 3"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "source": [],
    "metadata": {
     "collapsed": false
    }
   }
  }
 },
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "QKWvMXWMBEJA",
    "colab_type": "text"
   },
   "source": [
    "# Behavioural Planning for Autonomous Highway Driving\n",
    "\n",
    "We plan a trajectory using the _Optimistic Planning for Deterministic systems_ ([OPD](https://hal.inria.fr/hal-00830182)) algorithm.\n",
    "\n",
    "##  Setup\n",
    "\n",
    "We first import useful modules for the environment, agent, and visualization."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "s-ghXis6A_md",
    "colab_type": "code",
    "colab": {}
   },
   "source": [
    "# Environment\n",
    "!pip install highway-env\n",
    "import gym\n",
    "import highway_env\n",
    "\n",
    "# Agent\n",
    "!pip install git+https://github.com/eleurent/rl-agents#egg=rl-agents\n",
    "from rl_agents.agents.common.factory import agent_factory\n",
    "\n",
    "# Visualisation\n",
    "import sys\n",
    "from tqdm.notebook import trange\n",
    "!git clone https://github.com/eleurent/highway-env.git\n",
    "!pip install gym pyvirtualdisplay\n",
    "!apt-get install -y xvfb python-opengl ffmpeg\n",
    "sys.path.insert(0, './highway-env/scripts/')\n",
    "from utils import record_videos, show_videos, capture_intermediate_frames\n"
   ],
   "execution_count": 0,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "oi1SV025BXhB",
    "colab_type": "text"
   },
   "source": [
    "## Run an episode"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "bgNDDWwqCj8l",
    "colab_type": "code",
    "colab": {}
   },
   "source": [
    "# Make environment\n",
    "env = gym.make(\"highway-v0\")\n",
    "env = record_videos(env)\n",
    "obs, done = env.reset(), False\n",
    "capture_intermediate_frames(env)\n",
    "\n",
    "# Make agent\n",
    "agent_config = {\n",
    "    \"__class__\": \"<class 'rl_agents.agents.tree_search.deterministic.DeterministicPlannerAgent'>\",\n",
    "    \"env_preprocessors\": [{\"method\":\"simplify\"}],\n",
    "    \"budget\": 50,\n",
    "    \"gamma\": 0.7,\n",
    "}\n",
    "agent = agent_factory(env, agent_config)\n",
    "\n",
    "# Run episode\n",
    "for step in trange(env.unwrapped.config[\"duration\"], desc=\"Running...\"):\n",
    "    action = agent.act(obs)\n",
    "    obs, reward, done, info = env.step(action)\n",
    "    \n",
    "env.close()\n",
    "show_videos()"
   ],
   "execution_count": 0,
   "outputs": []
  }
 ]
}